package cn.com.open.itembank.sqlprovider;

import cn.com.open.itembank.entity.QuestionType;
import cn.com.open.itembank.entity.QuestionTypeUseRelation;
import cn.com.open.itembank.utils.StringUtil;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * Created by lucky on 2017/8/14.
 */
public class QuestionTypeSqlProvider {
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(QuestionTypeSqlProvider.class);

    /**
     * 题型生成insert的sql.
     *
     * @param model 题型信息
     * @return the insert
     */
    public String insert(final QuestionType model) {
        org.apache.ibatis.jdbc.SqlBuilder.BEGIN();
        INSERT_INTO("QuestionTypeDefine");
        if (model.getQuestionTypeID() != null) {
            VALUES("QuestionTypeID", "#{questionTypeID}");
        }
        if (model.getQuestionTypeName() != null) {
            VALUES("QuestionTypeName", "#{questionTypeName}");
        }
        if (model.getDirections() != null) {
            VALUES("Directions", "#{directions}");
        }
        VALUES("SerialNumber", "#{serialNumber}");
        VALUES("QuestionInnerTypeID", "#{questionInnerTypeID}");
        if (model.getSectionID() != null) {
            VALUES("SectionID", "#{sectionID}");
        }

        String sql = SQL();
        return sql;
    }

    /**
     * 题型使用关系生成insert的sql.
     *
     * @param model 题型使用关系信息
     * @return the insert
     */
    public String insertRelation(final QuestionTypeUseRelation model) {

        INSERT_INTO("QuestionTypeUseRelation");
        if (model.getQuestionTypeID() != null) {
            VALUES("QuestionTypeID", "#{questionTypeID}");
        }
        if (model.getID() != null) {
            VALUES("ID", "#{ID}");
        }
        if (model.getItemBankID() != null) {
            VALUES("ItemBankID", "#{itemBankID}");
        }
        VALUES("IsDefault", "#{isDefault}");
        VALUES("IsAppShare", "#{isAppShare}");
        if (model.getAppID() != null) {
            VALUES("AppID", "#{appID}");
        }
        VALUES("Status", "#{status}");
        VALUES("CreateTime", "#{createTime}");
        if (model.getCreater() != null) {
            VALUES("Creater", "#{creater}");
        }

        String sql = SQL();
        return sql;
    }

    /**
     * 根据内部题型ID,ID和题型名称，判断是否存在数据sql
     *
     * @param parameter the parameter
     * @return string string
     */
    public String isExistQuestionTypeName(Map<String, Object> parameter) {
        String id = (String) parameter.get("id");
        int innerTypeId = Integer.parseInt(parameter.get("innerTypeId").toString());
        String name = (String) parameter.get("name");

        StringBuilder sbSql = new StringBuilder();
        sbSql.append("SELECT COUNT(*) FROM QuestionTypeDefine");
        sbSql.append(String.format(" WHERE QuestionInnerTypeID=%d AND QuestionTypeName='%s'", innerTypeId, name));
        if (!StringUtil.isEmpty(id)) {
            sbSql.append(String.format(" AND QuestionTypeID <> '%s'", id));
        }

        String sql = sbSql.toString();
        return sql;
    }

    /**
     * 根据题库ID获取题型列表sql.
     *
     * @return the list by item bank id
     */
    public String getListByItemBankId(Map<String, Object> parameter) {
        boolean isAdmin = (boolean) parameter.get("admin");
        String name = (String) parameter.get("name");
        String itemBankID = (String) parameter.get("itemBankID");
        String appID = (String) parameter.get("appID");

        StringBuilder sbSql = new StringBuilder();
        sbSql.append("SELECT ");
        if (isAdmin) {
            sbSql.append(" define.QuestionTypeID,QuestionTypeName,QuestionInnerTypeID,Directions,SectionID,SerialNumber,IsDefault,IsAppShare,relation.Status,CreateTime");
        } else {
            sbSql.append(" define.*");
        }
        sbSql.append(" FROM QuestionTypeDefine define");
        sbSql.append(" INNER JOIN QuestionTypeUseRelation relation ON define.QuestionTypeID=relation.QuestionTypeID ");
        sbSql.append(String.format(" WHERE IsDefault=1 OR ItemBankID='%s' OR (AppID='%s' AND IsAppShare=1)", itemBankID, appID));
        if (isAdmin && !StringUtil.isEmpty(name)) {
            sbSql.append(" AND QuestionTypeName LIKE '%" + name + "%'");
        }
        if(!isAdmin)
        {
            sbSql.append(" AND Status <> -1");
        }
        sbSql.append(" ORDER BY QuestionInnerTypeID,SerialNumber");

        String sql = sbSql.toString();
        return sql;
    }
}
